<?php


namespace app\api\model;


use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
use app\common\enum\goods\Status as GoodsStatusEnum;
use app\common\model\Goods as GoodsModel;
use app\common\exception\BaseException;
use app\common\library\helper;
use think\Model;
use think\model\Collection;

class Goods extends Model
{

    /**
     * 关联图片表
     *
     */
    public function imgs()
    {
        return $this->hasMany('goods_image', 'goods_id', 'id');
    }
    /**
     * 关联商品图片表
     * @return \think\model\relation\HasMany
     */
    public function images()
    {
        return $this->hasMany('GoodsImage')->order(['id']);
    }

    /**
     * 关联商品规格表
     * @return \think\model\relation\HasMany
     */
    public function skuList()
    {
        return $this->hasMany('GoodsSku')->order(['id' => 'asc']);
    }
    /**
     * 关联商品规格表
     * @return \think\model\relation\HasMany
     */
    public function specList()
    {
        return $this->hasMany('GoodsSpecRel')->order(['id' => 'asc']);
    }

    /***
     * 获取到全部商品
     *
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getGoods()
    {
        $where = [];
        $goods = $this->with('imgs')->where($where)->select();
        return $goods;
    }


    /**
     * 获取推荐商品
     *
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRecGoods()
    {
        $goods = $this->with('imgs')->where('hot', 1)->select();
        return $goods;
    }

    /**
     * 获取指定分类下的商品
     *
     * @param $id
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCatePro($id)
    {
        $goods = $this->with('imgs')->where('category_id', $id)->select();
        return $goods;
    }

    /**
     * 根据搜索框输入内容获取匹配商品  标题和内容匹配任意一个
     *
     * @param $data
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function searchgood($data)
    {

        $goods = $this->with('imgs')->where('title|content', 'like', "%" . $data . "%")->select();
        return $goods;
    }

    /**
     * 获取商品详情
     *
     */
    public function details($goodsId)
    {
        // 获取商品记录
        $goodsInfo = $this->with('images,specList,skuList')->where('id',$goodsId)->find();
        if (empty($goodsInfo) || $goodsInfo['status'] == 0 ) {
           return false;
        }
        return $goodsInfo;
        // 设置商品展示的数据
        $goodsInfo = $this->setGoodsDataFromApi($goodsInfo);
        // 商品规格列表
        $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
        return $goodsInfo;
    }
    /**
     * 获取商品详情 (详细数据用于页面展示)
     * @param int $goodsId 商品id
     * @return mixed
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
//    public function getDetails(int $goodsId)
//    {
//        $goodsInfo = $this->with('images,skuList')->where('id',$goodsId)->find();
//        // 判断商品的状态
//        if (empty($goodsInfo) || $goodsInfo['is_delete'] || $goodsInfo['status'] != GoodsStatusEnum::ON_SALE) {
//            throwError('很抱歉，商品信息不存在或已下架');
//        }
//        // 设置商品展示的数据
//        $goodsInfo = $this->setGoodsDataFromApi($goodsInfo);
//        // 商品规格列表
//        $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
//        return $goodsInfo;
//    }
    /**
     * 整理商品数据 api模块
     * @param $goodsInfo
     * @return mixed
     */
    private function setGoodsDataFromApi($goodsInfo)
    {
        return $this->setGoodsData($goodsInfo, function ($goods) {
        });
    }
    /**
     * 整理商品数据
     * @param Collection|static $goodsInfo
     * @param callable|null $callback
     * @return mixed
     */
    protected function setGoodsData($goodsInfo, callable $callback = null)
    {
        // 商品图片列表
        $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
        // 商品主图
        $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
        // 商品销量(实际显示=初始虚拟销量+实际销量)
        $goodsInfo['goods_sales'] = $goodsInfo['sales_initial'] + $goodsInfo['sales_actual'];
        // 回调函数
        is_callable($callback) && call_user_func($callback, $goodsInfo);
        return $goodsInfo->hidden(array_merge($this->hidden, ['specRel', 'images']));
    }


}